'''此行没有高亮显示'''
'''第一次优化，改写为类'''
class Shop:
    def __init__(self,name):
        self.name = name
        self.products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
        self.product = {str(i):(v[0],v[1]) for i,v in enumerate(zip(*zip(*self.products)))}
        self.pro_cost = {i[0]:i[1] for i in zip(*zip(*self.products))}
        self._car = {}
        self.cost = 0
        self.invalid = 0
    def menu(self):
        print('\t商品列表\t'.center(28,'-'))
        print('{:8}\t{:8}\t{:8}'.format('序号', '商品', '单价（元）'))
        for ind in map(str,range(6)):
            print('{:8}\t{:8}\t{:8}'.format(ind, self.__dict__['product'][ind][0], self.__dict__['product'][ind][1]))
    def car(self):  #查看购物车
        if self.__dict__['_car']:
            print('\t购物车\t'.center(27, '-'))
            print('{:8}\t{:8}\t{:8}'.format('商品', '单价（元）','数量'))
            for k,v in self.__dict__['_car'].items():
                print('{:8}\t{:8}\t{:8}'.format(k,self.__dict__['pro_cost'][k],v))
        else:
            print('购物车为空')
    def settle(self):   #结算
        print('\t账单\t'.center(26, '-'))
        print('{:8}\t{:8}\t{:8}'.format('商品', '单价（元）', '数量'))
        for k, v in self.__dict__['_car'].items():
            print('{:8}\t{:8}\t{:8}'.format(k, self.__dict__['pro_cost'][k], v))
        print('{:>{}}'.format('总金额：{}'.format(self.cost),32))
    def option(self,inp:str):   #针对输入进行操作
        if self.product.get(inp,0):
            self._car[self.product[inp][0]] = self._car.get(inp,0)+1
            self.cost+=self.product[inp][1]
        elif inp == 'c':
            self.car()
        elif inp == 'g':
            self.menu()
        else:
            self.invalid+=1
            if self.invalid==2:
                print('由于您超过两次无效输入，已自动为您跳转')
                self.menu()
                i = input('请输入您要执行的操作：')
                self.option(i)
    def main(self): #运行函数
        self.menu()
        i = input('请输入您要执行的操作：')
        while i !='quit':
            self.option(i)
            i = input('请输入您要执行的操作：')
        self.settle()
        print('谢谢惠顾！'.center(32))

one= Shop('a')
one.main()








# def shop():
#     from collections import Counter
#     products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
#     product = []
#     cost = []
#     for i in zip(*zip(*products)):
#         product.append(i[0])
#         cost.append(i[1])
#     new = dict(zip(product,cost))
#     def settle():
#         cost = 0
#         print('-' * 9 + '购物明细' + '-' * 9)
#         c = Counter(shop_car)
#         [print('{}\t{}\t{:}  \t元'.format(v, i,new[i]*v)) for i,v in c.items()]
#         print()
#         for i in shop_cost:
#             cost += i
#         print('{:>{}}'.format('总开销：{}'.format(([cost])), 20))
#
#     def menu():
#         print('-' * 9 + '商品列表' + '-' * 9)
#         print('序号', '商品:', '单价（元）:', sep='\t')
#         for ind in range(6):
#             print(ind, products[ind][0], products[ind][1], sep='\t')
#
#     flag = 0
#     shop_car = []
#     shop_cost = []
#     while flag == 0:
#         menu()
#         print('欢迎使用本服务：', \
#               '\t1.购买某件商品请输入对应商品码',\
#               '\t2.查看购物车，请输入字母\'c\'',\
#               '\t3.退出购物并结算，请输入字母\'q\'',sep='\n')
#         inp = input('请输入：')
#         if inp == 'q':
#             flag -=1
#         elif inp == 'c' :
#             print('商品\t\t\t单价\t\t\t已购数量')
#             if len(set(shop_car))==0:
#                 print()
#                 print('您的购物车为空')
#             else:
#                 for i in set(shop_car):
#                     print('{:<10}{:>8.2f}{:>8}'.format(i,new[i],shop_car.count(i)))
#         elif inp in dict(zip(map(str,range(6)), products)):
#             shop_car.append(dict(zip(map(str,range(6)),products))[inp][0])
#             shop_cost.append(dict(zip(map(str,range(6)), products))[inp][1])
#             print(shop_car)
#             print(shop_cost)
#         else:
#             print('抱歉，没有这件商品或这件商品已售罄')
#             print('\t继续购物，请输入字母\'j\'',\
#                 '\t查看购物车，请输入字母\'c\'',\
#                 '\t退出购物并结算，请输入字母\'q\'',sep = '\n')
#             inp = input('请输入：')
#             if inp == 'q':
#                 flag -=1
#             elif inp in dict(zip(map(str,range(6)), products)):
#                 shop_car.append(dict(zip(map(str,range(6)),products))[inp][0])
#                 shop_cost.append(dict(zip(map(str,range(6)), products))[inp][1])
#                 print(shop_car)
#                 print(shop_cost)
#             elif  inp == 'c':
#                 print('商品\t\t单价\t\t已购数量')
#                 for i in sort(list(set(shop_car))):
#                     # sort 是什么？
#                     #就地修改的排序
#                     print('{}\t\t{}\t\t{}'.format(i,dict(zip(products))[i],shop_car.count(i)))
#             elif inp == 'j':
#                 pass
#             else:
#                 print('抱歉，由于您两次无效输入，已为您自动回退至商品选择。')
#     settle()
#
# shop()
#
# # 大的问题没有，逻辑不错。

